﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.Nutritionknowledge.RawManage
{
    public partial class RawEdit : System.Web.UI.Page
    {
        T_NutrientComposition tNutrientComposition = new T_NutrientComposition(); //元素操作
        T_RawMaterial tRawMaterial = new T_RawMaterial();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //显示营养元素值
        private void LoadData()
        {
            int RawID = int.Parse(Request.QueryString["RawID"].ToString());
            E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawID);
            if (eRawMaterial == null)
                return;

            this.txt_RawName.Text = eRawMaterial.RAWName;

            E_NutrientComposition md=new E_NutrientComposition();
            md.RawID=RawID;
            List<E_NutrientComposition> EntityList = tNutrientComposition.GetElementList(md);
            if (EntityList != null && EntityList.Count > 0)
            {
                foreach (Control c in this.Pan_EN.Controls) //遍历控件中的所有值
                {
                    TextBox tb = c as TextBox;
                    if (tb != null)
                    {
                        int NutritionElementsID = Convert.ToInt32(tb.ID.Replace("txt_", ""));
                        foreach (E_NutrientComposition item in EntityList)
                        {
                            if (item.NutritionElementsID == NutritionElementsID)
                            {
                                tb.Text = (item.Content == "" ? "0" : item.Content);
                            }
                        }
                    }
                }
            }
        }
        
        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawID = int.Parse(Request.QueryString["RawID"].ToString());
            
            //更新原料信息
            E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawID);
            eRawMaterial.RAWName = this.txt_RawName.Text.Trim();
            eRawMaterial.IsEdit = 1;//已修改状态
            tRawMaterial.Update(eRawMaterial);

            //删除对应的元素
            E_NutrientComposition eNutrientComposition=new E_NutrientComposition();
            eNutrientComposition.RawID=RawID;
            tNutrientComposition.RemoveByRawID(eNutrientComposition);

            //添加元素信息
            foreach (Control c in this.Pan_EN.Controls)
            {
                TextBox tb = c as TextBox;
                if (tb != null)
                {
                    E_NutrientComposition md = new E_NutrientComposition();
                    md.RawID = RawID;
                    md.NutritionElementsID = Convert.ToInt32(tb.ID.Replace("txt_", ""));
                    md.Content = tb.Text.Trim() == "" ? "0" : tb.Text.Trim();
                    tNutrientComposition.Add(md);
                }
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改成功！');</script>");
        }
    }
}